package iotwisp.school.api.controller.pc_admin.user;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import iotwisp.base.util.ValidationUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import iotwisp.base.cqrs.BusinessException;
import iotwisp.base.orm.query.pagination.Pagination;
import iotwisp.base.rest.JSONResultDTO;
import iotwisp.base.rest.RestBaseController;
import iotwisp.base.util.JSONUtils;
import iotwisp.school.user.command.role.ChangeRoleEnableSCmd;
import iotwisp.school.user.command.role.CreateRoleSCmd;
import iotwisp.school.user.command.role.DeleteRoleSCmd;
import iotwisp.school.user.command.role.ModifyRoleSCmd;
import iotwisp.school.user.entity.Role;
import iotwisp.school.user.qo.RoleQO;
import iotwisp.school.user.service.RoleService;

@RestController
@RequestMapping("/api/org/role")
public class RoleController extends RestBaseController {

	@Autowired
	private RoleService roleService;

	@RequestMapping(value = "/query", method = { RequestMethod.POST, RequestMethod.GET })
	public ResponseEntity<JSONResultDTO> queryRole(@RequestBody(required = false) RoleQO qoBody,
			@RequestParam(name = "qo", required = false) String qoJson) throws UnsupportedEncodingException {

		RoleQO qo;
		if (!StringUtils.isBlank(qoJson)) {
			qo = JSONUtils.parseObject(URLDecoder.decode(qoJson, "UTF-8"), RoleQO.class);
		} else {
			qo = qoBody;
		}

		if (qo.getFetchPermissions() != null && qo.getFetchPermissions()) {
			qo.setBatchResult(true);
		}

		switch (qo.getResultType()) {
		case RoleQO.RESULT_TYPE_COUNT:
			Integer count = roleService.queryCount(qo);
			return ResponseEntity.ok(createSuccessResult(count));
		case RoleQO.RESULT_TYPE_UNIQUE:
			Role role = roleService.queryUnique(qo);
			return ResponseEntity.ok(createSuccessResult(role));
		case RoleQO.RESULT_TYPE_LIST:
			List<Role> roles = roleService.queryList(qo);
			return ResponseEntity.ok(createSuccessResult(roles));
		case RoleQO.RESULT_TYPE_PAGINATION:
			Pagination pagination = roleService.queryPagination(qo);
			return ResponseEntity.ok(createSuccessResult(pagination));
		default:
			return ResponseEntity.badRequest()
					.body(createFailResult(BusinessException.NECESSARY_PARAM_NULL, "resultType值有误", null));
		}

	}

	@PostMapping("/create")
	public ResponseEntity<JSONResultDTO> createRole(@RequestBody CreateRoleSCmd cmd) throws BusinessException {

		ValidationUtils.validate(cmd);

		Role role = roleService.createRole(cmd);
		return ResponseEntity.ok(createSuccessResult(role));
	}

	@PutMapping("/modify")
	public ResponseEntity<JSONResultDTO> modifyRole(@RequestBody ModifyRoleSCmd cmd) throws BusinessException {

		ValidationUtils.validate(cmd);

		Role role = roleService.modifyRole(cmd);
		return ResponseEntity.ok(createSuccessResult(role));
	}

	@PutMapping("/change_enable")
	public ResponseEntity<JSONResultDTO> changeRoleEnable(@RequestBody ChangeRoleEnableSCmd cmd)
			throws BusinessException {

		roleService.changeRoleEnable(cmd);
		return ResponseEntity.ok(createSuccessResult(null));
	}

	@DeleteMapping("/delete")
	public ResponseEntity<JSONResultDTO> deleteRole(@RequestBody DeleteRoleSCmd cmd) throws BusinessException {

		roleService.deleteRole(cmd);
		return ResponseEntity.ok(createSuccessResult(null));
	}
}
